﻿using System;
using System.Windows.Forms;
using DesignerHelper.Addons;
using SpreadsheetGear;

namespace WorkbookDesigner.Addons.WorkbookExplorer
{
    public partial class PageSetupMain : UserControl, IMarine<IPageSetup>
    {
        public PageSetupMain()
        {
            InitializeComponent();
        }

        public PageSetupMain(IPageSetup pageSetup) : this()
        {
            TriggerInit(pageSetup);
        }

        private BaseTab Bt { get; set; }
        private PageSetupContainer Psc { get; set; }
        private IPageSetup PageSetup { get; set; }

        #region 封装变量

        private PageOrientation SelectedOrientation
        {
            get
            {
                if (radioButtonPortrait.Checked && !radioButtonLandscape.Checked)
                    return PageOrientation.Portrait;
                if (radioButtonLandscape.Checked && !radioButtonPortrait.Checked)
                    return PageOrientation.Landscape;
                return PageOrientation.Portrait;
            }
        }

        private int SelectedZoomValue
        {
            get
            {
                //return (int)numericZoom.Value;
                return Convert.ToInt32(numericZoom.Value);
            }
            set { numericZoom.Value = value; }
        }

        private int SelectedFitTall
        {
            get { return Convert.ToInt32(numericTall.Value); }
            set { numericTall.Value = value; }
        }

        private int SelectedFitWide
        {
            get { return Convert.ToInt32(numericWide.Value); }
            set { numericWide.Value = value; }
        }

        private int SelectedFirstPageNumber
        {
            get { return int.Parse(FirstPageNum.Text); }
            set { FirstPageNum.Text = value.ToString(); }
        }

        #endregion

        public void TriggerInit(IPageSetup ps)
        {
            PageSetup = ps;
            Init();
        }

        public void TriggerReset()
        {
            Init();
        }

        public void TriggerSet()
        {
            SetOrientation();
            SetFPageNum();
            SetZoom();
            SetFitPage();
        }

        private void Init()
        {
            InitOrientation();
            InitScaling();
            InitOptions();
        }

        #region Init

        private void InitOrientation()
        {
            switch (PageSetup.Orientation)
            {
                case PageOrientation.Portrait:
                    radioButtonPortrait.Checked = true;
                    break;

                case PageOrientation.Landscape:
                    radioButtonLandscape.Checked = true;
                    break;

                default:
                    break;
            }
        }

        private void InitScaling()
        {
            if (!PageSetup.FitToPages)
            {
                numericZoom.Value = PageSetup.Zoom;
                radioButtonZoom.Checked = true;
            }
            else if (PageSetup.FitToPages)
            {
                radioButtonFitTo.Checked = true;
                numericWide.Value = PageSetup.FitToPagesWide;
                numericTall.Value = PageSetup.FitToPagesTall;
            }
        }

        private void InitOptions()
        {
            SelectedFirstPageNumber = PageSetup.FirstPageNumber;
        }

        #endregion Init

        #region Setting

        private void SetOrientation()
        {
            PageSetup.Orientation = SelectedOrientation;
        }

        private void SetZoom()
        {
            if (!radioButtonZoom.Checked)
                return;
            PageSetup.FitToPages = false;
            PageSetup.Zoom = SelectedZoomValue;
        }

        private void SetFitPage()
        {
            if (!radioButtonFitTo.Checked)
                return;
            PageSetup.FitToPages = true;
            PageSetup.FitToPagesTall = SelectedFitTall;
            PageSetup.FitToPagesWide = SelectedFitWide;
        }

        private void SetFPageNum()
        {
            PageSetup.FirstPageNumber = SelectedFirstPageNumber;
        }

        #endregion Setting

        #region interface

        void IScv<IPageSetup>.TriggerReset()
        {
            TriggerReset();
        }

        void IScv<IPageSetup>.TriggerSet()
        {
            TriggerSet();
        }

        Control IScv<IPageSetup>.GetControl()
        {
            return this;
        }

        void IMarine<IPageSetup>.TriggerInit(IPageSetup sendIn)
        {
            TriggerInit(sendIn);
        }

        #endregion
    }
}